#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
最终测试排列5预测修复
"""

import sys
import os
from PyQt5.QtWidgets import QApplication

# 添加项目根目录到路径
project_root = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, project_root)

def final_test_plw_fix():
    """最终测试排列5预测修复"""
    print("🎯 最终测试排列5预测修复")
    print("=" * 60)
    
    try:
        from lottery_predictor_app import LotteryPredictorApp
        
        # 创建应用
        if not QApplication.instance():
            app = QApplication(sys.argv)
        
        predictor = LotteryPredictorApp()
        
        print("📋 测试1: LSTM-CRF经典模式预测...")
        try:
            predictions = predictor.get_lstm_crf_predictions("plw", 2)
            
            if predictions and len(predictions) > 0:
                print(f"✅ LSTM-CRF预测成功，生成 {len(predictions)} 组结果")
                for i, pred in enumerate(predictions):
                    red = pred.get('red', [])
                    blue = pred.get('blue', [])
                    print(f"  第{i+1}组: 数字 {red} 蓝球 {blue}")
                    
                    # 验证结果格式
                    if not isinstance(red, list) or len(red) != 5:
                        print(f"  ⚠️  红球格式不正确: {red}")
                        return False
                    
                    if not isinstance(blue, list) or len(blue) != 0:
                        print(f"  ⚠️  蓝球格式不正确: {blue}")
                        return False
                        
                    # 验证数字范围
                    for num in red:
                        if not isinstance(num, int) or num < 0 or num > 9:
                            print(f"  ⚠️  数字范围不正确: {num}")
                            return False
                            
                    print(f"  ✅  第{i+1}组格式正确")
            else:
                print(" LSTM-CRF预测失败，未生成任何结果")
                return False
                
        except Exception as e:
            print(f" LSTM-CRF预测异常: {e}")
            import traceback
            traceback.print_exc()
            return False
        
        print("\n📋 测试2: 序列LSTM增强模式预测...")
        try:
            predictions = predictor.get_enhanced_lstm_predictions("plw", 2)
            
            if predictions and len(predictions) > 0:
                print(f"✅ 序列LSTM增强模式预测成功，生成 {len(predictions)} 组结果")
                for i, pred in enumerate(predictions):
                    red = pred.get('red', [])
                    blue = pred.get('blue', [])
                    confidence = pred.get('confidence', 0)
                    print(f"  第{i+1}组: 数字 {red} 蓝球 {blue} 置信度 {confidence:.3f}")
                    
                    # 验证结果格式
                    if not isinstance(red, list) or len(red) != 5:
                        print(f"  ⚠️  红球格式不正确: {red}")
                        return False
                    
                    if not isinstance(blue, list) or len(blue) != 0:
                        print(f"  ⚠️  蓝球格式不正确: {blue}")
                        return False
                        
                    # 验证数字范围
                    for num in red:
                        if not isinstance(num, int) or num < 0 or num > 9:
                            print(f"  ⚠️  数字范围不正确: {num}")
                            return False
                            
                    # 验证置信度
                    if not isinstance(confidence, (int, float)) or confidence < 0 or confidence > 1:
                        print(f"  ⚠️  置信度格式不正确: {confidence}")
                        return False
                        
                    print(f"  ✅  第{i+1}组格式正确")
            else:
                print("⚠️ 序列LSTM增强模式预测未生成结果（可能模型不可用）")
                
        except Exception as e:
            print(f" 序列LSTM增强模式预测异常: {e}")
            import traceback
            traceback.print_exc()
            return False
        
        print("\n📋 测试3: 梯度提升特征模式预测...")
        try:
            predictions = predictor.get_gradient_boost_predictions("plw", 2)
            
            if predictions and len(predictions) > 0:
                print(f"✅ 梯度提升特征模式预测成功，生成 {len(predictions)} 组结果")
                for i, pred in enumerate(predictions):
                    red = pred.get('red', [])
                    blue = pred.get('blue', [])
                    print(f"  第{i+1}组: 数字 {red} 蓝球 {blue}")
                    
                    # 验证结果格式
                    if not isinstance(red, list) or len(red) != 5:
                        print(f"  ⚠️  红球格式不正确: {red}")
                        return False
                    
                    if not isinstance(blue, list) or len(blue) != 0:
                        print(f"  ⚠️  蓝球格式不正确: {blue}")
                        return False
                        
                    # 验证数字范围
                    for num in red:
                        if not isinstance(num, int) or num < 0 or num > 9:
                            print(f"  ⚠️  数字范围不正确: {num}")
                            return False
                            
                    print(f"  ✅  第{i+1}组格式正确")
            else:
                print(" 梯度提升特征模式预测失败，未生成任何结果")
                return False
                
        except Exception as e:
            print(f" 梯度提升特征模式预测异常: {e}")
            import traceback
            traceback.print_exc()
            return False
        
        print("\n📋 测试4: 模式记忆网络模式预测...")
        try:
            predictions = predictor.get_memory_network_predictions("plw", 2)
            
            if predictions and len(predictions) > 0:
                print(f"✅ 模式记忆网络模式预测成功，生成 {len(predictions)} 组结果")
                for i, pred in enumerate(predictions):
                    red = pred.get('red', [])
                    blue = pred.get('blue', [])
                    print(f"  第{i+1}组: 数字 {red} 蓝球 {blue}")
                    
                    # 验证结果格式
                    if not isinstance(red, list) or len(red) != 5:
                        print(f"  ⚠️  红球格式不正确: {red}")
                        return False
                    
                    if not isinstance(blue, list) or len(blue) != 0:
                        print(f"  ⚠️  蓝球格式不正确: {blue}")
                        return False
                        
                    # 验证数字范围
                    for num in red:
                        if not isinstance(num, int) or num < 0 or num > 9:
                            print(f"  ⚠️  数字范围不正确: {num}")
                            return False
                            
                    print(f"  ✅  第{i+1}组格式正确")
            else:
                print(" 模式记忆网络模式预测失败，未生成任何结果")
                return False
                
        except Exception as e:
            print(f" 模式记忆网络模式预测异常: {e}")
            import traceback
            traceback.print_exc()
            return False
        
        print("\n📋 测试5: 权重专家系统模式预测...")
        try:
            predictions = predictor.get_weighted_expert_predictions("plw", 2)
            
            if predictions and len(predictions) > 0:
                print(f"✅ 权重专家系统模式预测成功，生成 {len(predictions)} 组结果")
                for i, pred in enumerate(predictions):
                    red = pred.get('red', [])
                    blue = pred.get('blue', [])
                    print(f"  第{i+1}组: 数字 {red} 蓝球 {blue}")
                    
                    # 验证结果格式
                    if not isinstance(red, list) or len(red) != 5:
                        print(f"  ⚠️  红球格式不正确: {red}")
                        return False
                    
                    if not isinstance(blue, list) or len(blue) != 0:
                        print(f"  ⚠️  蓝球格式不正确: {blue}")
                        return False
                        
                    # 验证数字范围
                    for num in red:
                        if not isinstance(num, int) or num < 0 or num > 9:
                            print(f"  ⚠️  数字范围不正确: {num}")
                            return False
                            
                    print(f"  ✅  第{i+1}组格式正确")
            else:
                print(" 权重专家系统模式预测失败，未生成任何结果")
                return False
                
        except Exception as e:
            print(f" 权重专家系统模式预测异常: {e}")
            import traceback
            traceback.print_exc()
            return False
        
        print("\n🎉 所有测试通过！排列5多算法预测功能已完全修复！")
        return True
        
    except Exception as e:
        print(f" 测试失败: {e}")
        import traceback
        traceback.print_exc()
        return False

if __name__ == "__main__":
    success = final_test_plw_fix()
    if success:
        print("\n✅ 排列5多算法预测修复完成！")
    else:
        print("\n❌ 排列5多算法预测修复未完成！")